package com.cheyunkeji.er.activity.auction;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.cheyun.netsalev3.MyApplication;
import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.CarArchiveDetailBean;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.utils.TimeUtils;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TabTitle;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.auction.SimpleItemTextView;
import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;

public class CarArchiveDetailActivity extends BaseActivity {


    private static final String TAG = CarArchiveDetailActivity.class.getSimpleName();
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.tt_car_basic_info)
    TabTitle ttCarBasicInfo;
    @BindView(R.id.sitv_ccny)
    SimpleItemTextView sitvCcny;
    @BindView(R.id.sitv_csys)
    SimpleItemTextView sitvCsys;
    @BindView(R.id.sitv_pfbz)
    SimpleItemTextView sitvPfbz;
    @BindView(R.id.sitv_pl)
    SimpleItemTextView sitvPl;
    @BindView(R.id.sitv_scspr)
    SimpleItemTextView sitvScspr;
    @BindView(R.id.sitv_syxz)
    SimpleItemTextView sitvSyxz;
    @BindView(R.id.sitv_bsx)
    SimpleItemTextView sitvBsx;
    @BindView(R.id.sitv_rllx)
    SimpleItemTextView sitvRllx;
    @BindView(R.id.sitv_qdfs)
    SimpleItemTextView sitvQdfs;
    @BindView(R.id.sitv_gs)
    SimpleItemTextView sitvGs;
    @BindView(R.id.sitv_zws)
    SimpleItemTextView sitvZws;
    @BindView(R.id.sitv_njdq)
    SimpleItemTextView sitvNjdq;
    @BindView(R.id.sitv_jqxdq)
    SimpleItemTextView sitvJqxdq;
    @BindView(R.id.sitv_ccsdq)
    SimpleItemTextView sitvCcsdq;
    @BindView(R.id.sv_content)
    ScrollView svContent;
    @BindView(R.id.activity_car_archive_detail)
    LinearLayout activityCarArchiveDetail;
    @BindView(R.id.ll_car_basic_info_wrapper)
    LinearLayout llCarBasicInfoWrapper;
    @BindView(R.id.tt_car_related_info)
    TabTitle ttCarRelatedInfo;
    @BindView(R.id.sitv_gzszs)
    SimpleItemTextView sitvGzszs;
    @BindView(R.id.sitv_xcfp)
    SimpleItemTextView sitvXcfp;
    @BindView(R.id.sitv_jdcxsz)
    SimpleItemTextView sitvJdcxsz;
    @BindView(R.id.sitv_jdcdjzs)
    SimpleItemTextView sitvJdcdjzs;
    @BindView(R.id.sitv_ccsp)
    SimpleItemTextView sitvCcsp;
    @BindView(R.id.sitv_sybx)
    SimpleItemTextView sitvSybx;
    @BindView(R.id.sitv_ghcs)
    SimpleItemTextView sitvGhcs;
    @BindView(R.id.sitv_wclwzjl)
    SimpleItemTextView sitvWclwzjl;
    @BindView(R.id.sitv_cfdy)
    SimpleItemTextView sitvCfdy;
    @BindView(R.id.ll_car_related_info_wrapper)
    LinearLayout llCarRelatedInfoWrapper;
    @BindView(R.id.tt_car_config_info)
    TabTitle ttCarConfigInfo;
    @BindView(R.id.sitv_cc)
    SimpleItemTextView sitvCc;
    @BindView(R.id.sitv_py)
    SimpleItemTextView sitvPy;
    @BindView(R.id.sitv_abs)
    SimpleItemTextView sitvAbs;
    @BindView(R.id.sitv_ddzy)
    SimpleItemTextView sitvDdzy;
    @BindView(R.id.sitv_ddhsj)
    SimpleItemTextView sitvDdhsj;
    @BindView(R.id.sitv_lhjlg)
    SimpleItemTextView sitvLhjlg;
    @BindView(R.id.sitv_fdq)
    SimpleItemTextView sitvFdq;
    @BindView(R.id.sitv_dlzx)
    SimpleItemTextView sitvDlzx;
    @BindView(R.id.sitv_tc)
    SimpleItemTextView sitvTc;
    @BindView(R.id.sitv_dcld)
    SimpleItemTextView sitvDcld;
    @BindView(R.id.sitv_zddc)
    SimpleItemTextView sitvZddc;
    @BindView(R.id.sitv_dcxy)
    SimpleItemTextView sitvDcxy;
    @BindView(R.id.sitv_dsxh)
    SimpleItemTextView sitvDsxh;
    @BindView(R.id.sitv_wxdh)
    SimpleItemTextView sitvWxdh;
    @BindView(R.id.sitv_znys)
    SimpleItemTextView sitvZnys;
    @BindView(R.id.sitv_csmp)
    SimpleItemTextView sitvCsmp;
    @BindView(R.id.sitv_yxzj)
    SimpleItemTextView sitvYxzj;
    @BindView(R.id.sitv_cd)
    SimpleItemTextView sitvCd;
    @BindView(R.id.sitv_dvd)
    SimpleItemTextView sitvDvd;
    @BindView(R.id.sitv_mp3)
    SimpleItemTextView sitvMp3;
    @BindView(R.id.sitv_tv)
    SimpleItemTextView sitvTv;
    @BindView(R.id.sitv_xcjly)
    SimpleItemTextView sitvXcjly;
    @BindView(R.id.sitv_qns)
    SimpleItemTextView sitvQns;
    @BindView(R.id.sitv_ktks)
    SimpleItemTextView sitvKtks;
    @BindView(R.id.sitv_ddms)
    SimpleItemTextView sitvDdms;
    @BindView(R.id.sitv_yjps)
    SimpleItemTextView sitvYjps;
    @BindView(R.id.sitv_drive_miles)
    SimpleItemTextView sitvDriveMiles;
    @BindView(R.id.sitv_dp)
    SimpleItemTextView sitvDp ;
    @BindView(R.id.ll_car_config_info_wrapper)
    LinearLayout llCarConfigInfoWrapper;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_car_archive_detail);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {

        vTopbar.setLeftBack();
        vTopbar.setTitle("车辆档案详情");
        ttCarBasicInfo.setTitle("车辆基本信息");
        ttCarBasicInfo.setView(llCarBasicInfoWrapper, svContent);

        ttCarConfigInfo.setTitle("车辆配置信息");
        ttCarConfigInfo.setView(llCarConfigInfoWrapper, svContent);

        ttCarRelatedInfo.setTitle("车辆相关信息");
        ttCarRelatedInfo.setView(llCarRelatedInfoWrapper, svContent);

    }

    @Override
    protected void initData() {

        String bid = getIntent().getStringExtra("bid");
        Log.e(TAG, "initData:  BID : " + bid);
        if (!TextUtils.isEmpty(bid)) {
            loadArchiveDetailInfo(bid);
        }
    }

    private void loadArchiveDetailInfo(String bid) {

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
        params.put("bid", bid);

        ApiClient.postForm(HttpConstants.GET_VEHICLE_INFO, params, new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                        if (e instanceof UnknownHostException || e instanceof SocketException) {
                            SToast.show(R.string.tip_network_error, Gravity.CENTER);
                            return;
                        }
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        try {
                            JSONObject obj = new JSONObject(response);
                            if (obj.optInt("code", 0) == 1) {

                                CarArchiveDetailBean detailBean = new Gson().fromJson(obj.optString("data"), CarArchiveDetailBean.class);
                                displayData(detailBean);

                            } else {
                                SToast.show(obj.optString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        showDialog();
                    }

                    @Override
                    public void onAfter(int id) {
                        dismissDialog();
                    }

                }

        );

    }

    private void displayData(CarArchiveDetailBean data) {

//      出厂日期
        if (!TextUtils.isEmpty(data.getExwdate())) {
        }
        sitvCcny.setContent(TimeUtils.millis10bitToStringWithoutHMS(data.getExwdate()));

        sitvCsys.setContent(data.getBodystyle_name());

        sitvPfbz.setContent(data.getEmission_name());

        if (!TextUtils.isEmpty(String.valueOf(data.getDisplace())) && !String.valueOf(data.getDisplace()).equals("0")) {
            sitvPl.setContent(new StringBuilder(String.valueOf(data.getDisplace())).append(" ").append(data.getDispunit()).toString());
        }

//       首次上牌日
        if (!TextUtils.isEmpty(data.getLicdate())) {
            sitvScspr.setContent(TimeUtils.millis10bitToStringWithoutHMS(data.getLicdate()));
        }

//      使用性质
        sitvSyxz.setContent(MyApplication.getInstance().getCarTempAttrList().getAr_isuse().get(Integer.valueOf(data.getIsuse())));

//        变速箱
        sitvBsx.setContent(data.getTrans_name());

//      燃料类型
        sitvRllx.setContent(data.getFueltype_name());

//      驱动方式
        sitvQdfs.setContent(data.getDrivetrain_name());

//      缸数
        sitvGs.setContent(data.getCylinders_name());

//      座位数
        sitvZws.setContent(data.getSeats_name());

//      年检到期
        if (!TextUtils.isEmpty(data.getVaidate()) && !data.getVaidate().equals("0"))
            sitvNjdq.setContent(TimeUtils.millis10bitToStringWithoutHMS(data.getVaidate()));
//      交强险到期
        if (!TextUtils.isEmpty(data.getTcidate()) && !data.getTcidate().equals("0"))
            sitvJqxdq.setContent(TimeUtils.millis10bitToStringWithoutHMS(data.getTcidate()));

//      车船税到期
        sitvGzszs.setContent(TextUtils.equals(data.getVpt(), "1") ? "有" : "无");
        sitvXcfp.setContent(TextUtils.equals(data.getInvoice(), "1") ? "有" : "无");
        sitvJdcxsz.setContent(TextUtils.equals(data.getVelic(), "1") ? "有" : "无");
        sitvJdcdjzs.setContent(TextUtils.equals(data.getVrlic(), "1") ? "有" : "无");
        sitvCcsp.setContent(TextUtils.equals(data.getCcsp(), "1") ? "有" : "无");
        sitvSybx.setContent(TextUtils.equals(data.getVci(), "1") ? "有" : "无");
        sitvXcfp.setContent(TextUtils.equals(data.getInvoice(), "1") ? "有" : "无");
        sitvWclwzjl.setContent(TextUtils.equals(data.getNorecord(), "1") ? "有" : "无");
        sitvCfdy.setContent(TextUtils.equals(data.getMortgage(), "1") ? "有" : "无");
        sitvDp.setContent(TextUtils.equals(data.getCarryplate(),"1") ? "有" : "无");

        sitvGhcs.setContent(data.getTransfers());
        sitvCc.setContent(TextUtils.equals(data.getSunroof(), "1") ? "有" : "无");
        sitvPy.setContent(TextUtils.equals(data.getLeatherchair(), "1") ? "有" : "无");

        sitvAbs.setContent(TextUtils.equals(data.getAbs(), "1") ? "有" : "无");
        sitvDdzy.setContent(TextUtils.equals(data.getAutochair(), "1") ? "有" : "无");
        sitvDdhsj.setContent(TextUtils.equals(data.getPdmirrors(), "1") ? "有" : "无");
        sitvLhjlg.setContent(TextUtils.equals(data.getAlloywheel(), "1") ? "有" : "无");
        sitvFdq.setContent(TextUtils.equals(data.getCaralarm(), "1") ? "有" : "无");
        sitvDlzx.setContent(TextUtils.equals(data.getPps(), "1") ? "有" : "无");
        sitvTc.setContent(TextUtils.equals(data.getSteelwheels(), "1") ? "有" : "无");

        sitvDcld.setContent(TextUtils.equals(data.getPdc(), "1") ? "有" : "无");
        sitvZddc.setContent(TextUtils.equals(data.getPav(), "1") ? "有" : "无");
        sitvDcxy.setContent(TextUtils.equals(data.getReverseimg(), "1") ? "有" : "无");
        sitvDsxh.setContent(TextUtils.equals(data.getCcs(), "1") ? "有" : "无");
        sitvWxdh.setContent(TextUtils.equals(data.getAvigation(), "1") ? "有" : "无");
        sitvZnys.setContent(TextUtils.equals(data.getSmartkey(), "1") ? "有" : "无");
        sitvCsmp.setContent(TextUtils.equals(data.getEmblem(), "1") ? "有" : "无");
        sitvYxzj.setContent(TextUtils.equals(data.getSpeker(), "1") ? "有" : "无");
        sitvCd.setContent(TextUtils.equals(data.getCd(), "1") ? "有" : "无");
        sitvDvd.setContent(TextUtils.equals(data.getDvd(), "1") ? "有" : "无");
        sitvMp3.setContent(TextUtils.equals(data.getMp3(), "1") ? "有" : "无");
        sitvTv.setContent(TextUtils.equals(data.getCmtv(), "1") ? "有" : "无");
        sitvXcjly.setContent(TextUtils.equals(data.getCardvr(), "1") ? "有" : "无");

        if (!TextUtils.isEmpty(data.getAirbag())) {
            sitvQns.setContent(data.getAirbag());
        }
        if (!TextUtils.isEmpty(data.getAcnum()))
            sitvKtks.setContent(data.getAcnum());
        if (!TextUtils.isEmpty(data.getAutodoor())) {
            sitvDdms.setContent(data.getAutodoor());
        }
        if (!TextUtils.isEmpty(data.getLcdnum())) {
            sitvYjps.setContent(data.getLcdnum());
        }
        if(!TextUtils.isEmpty(data.getMileage())){
            sitvDriveMiles.setContent(String.format("%1$s万公里" , data.getMileage() ));
        }

    }


}
